// 获取cookie
export function getCookie (name) {
   let arr
   let reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
   if (document.cookie.match(reg)) {
      arr = document.cookie.match(reg)
      return decodeURIComponent(arr[2])
   } else {
      return null
   }
}

// 设置cookie,增加到vue实例方便全局调用
export function setCookie (cName, value, expiredays = 1) {
   let exdate = new Date()
   exdate.setDate(exdate.getDate() + expiredays)
   document.cookie = cName + '=' + encodeURIComponent(value) + ((expiredays == null) ? '' : ';expires=' + exdate.toGMTString())
}

// 删除指定的cookie
export function delCookie (name) {
   let exp = new Date()
   exp.setTime(exp.getTime() - 1)
   let cval = getCookie(name)
   if (cval !== null) {
      document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString()
   }
}

// 删除所有的cookie
export function clearAllCookie () {
   var keys = document.cookie.match(/[^ =;]+(?==)/g)
   if (keys) {
      for (var i = keys.length; i--;) {
         document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
      }
   }
}
